package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goodstype;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;

public class GoodstypeDaoImpl implements GoodstypeDao {

	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs =null;
	
	@Override
	public boolean addGoodstype(Goodstype goodstype) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO GOODSTYPE(GOODSTYPE_NAME,USER_ID) VALUES(?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, goodstype.getGoodstypeName());
			ps.setLong(2,goodstype.getUser().getUserId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean delGoodstype(Long id) {
		// TODO Auto-generated method stub

		String sql = "DELETE FROM GOODSTYPE WHERE GOODSTYPE_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateGoodstype(Goodstype goodstype) {
		// TODO Auto-generated method stub
		String sql = "UPDATE GOODSTYPE SET GOODSTYPE_NAME=?,USER_ID=? " +
				"WHERE GOODSTYPE_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, goodstype.getGoodstypeName());
			ps.setLong(2, goodstype.getUser().getUserId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	
	@Override
	public List<Goodstype> getListPage(int currentPage, int pageSize,
			String namekey) {
		// TODO Auto-generated method stub
		List<Goodstype> list = new ArrayList<Goodstype>();
		String sql = "SELECT * FROM GOODSTYPE G LEFT JOIN USER U ON G.USER_ID=U.USER_ID WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND GOODSTYPE_NAME LIKE '%"+namekey+"%' ";
		}
			sql = sql +	" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Goodstype gs = new Goodstype();
				gs.setGoodstypeId(rs.getLong("GOODSTYPE_ID"));
				gs.setGoodstypeName(rs.getString("GOODSTYPE_NAME"));
				User user = new User();
				user.setUserId(rs.getLong("USER_ID"));
				gs.setUser(user);
				list.add(gs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount(String namekey) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM GOODSTYPE WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND GOODSTYPE_NAME LIKE '%"+namekey+"%' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public List<Goodstype> getAllList() {
		// TODO Auto-generated method stub
		List<Goodstype> list = new ArrayList<Goodstype>();
		String sql = "SELECT * FROM GOODSTYPE G LEFT JOIN USER U ON G.USER_ID=U.USER_ID";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			while(rs.next()){
				Goodstype gs = new Goodstype();
				gs.setGoodstypeId(rs.getLong("GOODSTYPE_ID"));
				gs.setGoodstypeName(rs.getString("GOODSTYPE_NAME"));
				User user = new User();
				user.setUserId(rs.getLong("USER_ID"));
				gs.setUser(user);
				list.add(gs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

}
